//
//  ZMConfigTools.m
//  YYFMMall
//
//  Created by chenzm on 2019/8/26.
//  Copyright © 2019 YuYou1. All rights reserved.
//

#import "ZMConfigTools.h"
#import <IQKeyboardManager.h>
#import <AVKit/AVKit.h>
//#import <UMMobClick/MobClick.h>



//#import "JSHAREService.h"

// 引入 JPush 功能所需头文件
//#import <JPUSHService.h>
// iOS10 注册 APNs 所需头文件
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif

@interface ZMConfigTools()

@end

@implementation ZMConfigTools

#pragma mark- Pods工具配置
/// 键盘设置
+ (void)configKeyBoard{
    IQKeyboardManager *manager = [IQKeyboardManager sharedManager];
    manager.enable = YES;
    manager.shouldResignOnTouchOutside = YES;
    manager.shouldToolbarUsesTextFieldTintColor = YES;
    manager.enableAutoToolbar = NO;
}

/// 高德地图配置
+(void)configGouldMap{
//    [AMapServices sharedServices].apiKey = kGouldMapApiKey;
}



#pragma mark- 视频播放及处理
/// 全屏视频播放
+ (void)zm_playVideoWithUrl:(NSString *)url target:(UIViewController *)target{
    NSURL *videoURL = [NSURL new];
    if ([url containsString:@"http"]) {
        videoURL = [NSURL URLWithString:url];
    }else{
        videoURL = [NSURL fileURLWithPath:url];
    }
    //步骤2：创建AVPlayer
    AVPlayer *avPlayer = [[AVPlayer alloc] initWithURL:videoURL];
    //步骤3：使用AVPlayer创建AVPlayerViewController，并跳转播放界面
    AVPlayerViewController *avPlayerVC =[[AVPlayerViewController alloc] init];
    avPlayerVC.player = avPlayer;
    [avPlayerVC.player play];
    [target presentViewController:avPlayerVC animated:YES completion:nil];
}

/// 在视图播放视频
+ (void)zm_playVideoWithUrl:(NSString *)urlStr toView:(UIView *)toView{
    NSURL *videoURL = [NSURL new];
    if ([urlStr containsString:@"http"]) {
        videoURL = [NSURL URLWithString:urlStr];
    }else{
        videoURL = [NSURL fileURLWithPath:urlStr];
    }
    AVPlayer *player = [AVPlayer playerWithURL:videoURL];
    AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
    playerLayer.frame = toView.bounds;
    [toView.layer addSublayer:playerLayer];
    [player play];
}

/**
 *  视频压缩
 *  @param url 视频文件的url
 *  @return 压缩后的url
 */
+ (NSURL *)zm_condenseVideoNewUrl:(NSURL *)url{
    // 沙盒目录
    NSString *docuPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    NSString *destFilePath = [docuPath stringByAppendingPathComponent:[NSString stringWithFormat:@"his%@.mp4",[ZMSpeedy changeStrFromDate:[NSDate date] dateFormat:@"yyyyMMddHHmmss"]]];
    NSURL *destUrl = [NSURL fileURLWithPath:destFilePath];
    //将视频文件copy到沙盒目录中
    NSFileManager *manager = [NSFileManager defaultManager];
    NSError *error = nil;
    [manager copyItemAtURL:url toURL:destUrl error:&error];
    NSLog(@"压缩前--%.2fM",[ZMSpeedy folderSizeAtPath:destFilePath]);
    // 进行压缩
    AVAsset *asset = [AVAsset assetWithURL:destUrl];
    //创建视频资源导出会话
    /**
     NSString *const AVAssetExportPresetLowQuality; // 低质量
     NSString *const AVAssetExportPresetMediumQuality;
     NSString *const AVAssetExportPresetHighestQuality; //高质量
     */
    AVAssetExportSession *session = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetLowQuality];
    // 创建导出的url
    NSString *resultPath = [docuPath stringByAppendingPathComponent:[NSString stringWithFormat:@"new%@.mp4",[ZMSpeedy changeStrFromDate:[NSDate date] dateFormat:@"yyyyMMddHHmmss"]]];
    session.outputURL = [NSURL fileURLWithPath:resultPath];
    // 必须配置输出属性
    session.outputFileType = @"com.apple.quicktime-movie";
    // 导出视频
    [session exportAsynchronouslyWithCompletionHandler:^{
        NSLog(@"压缩后---%.2fM",[ZMSpeedy folderSizeAtPath:resultPath]);
        
    }];
    return session.outputURL;
}

#pragma mark- 数据转换
/// 数组转字符串
+(NSString *)zm_arrayToJson:(NSArray *)arr{
    NSMutableString *mStr = [NSMutableString new];
    [mStr appendString:@""];
    for (NSInteger i = 0; i < arr.count; i ++) {
        [mStr appendString:arr[i]];
        if (i != arr.count - 1) {
            [mStr appendString:@"^"];
        }
    }
    return mStr.copy;
}

/// 字符串转数组
+(NSArray *)zm_jsonToArray:(NSString *)str{
    NSArray *arr = [str componentsSeparatedByString:@"^"];
    NSMutableArray *marr = [NSMutableArray arrayWithCapacity:0];
    for (NSInteger i = 0; i < arr.count; i ++) {
        NSString *tempStr = arr[i];
        if (tempStr.length > 0) {
            [marr addObject:tempStr];
        }
    }
    return marr.copy;
}

#pragma mark- 内部应用
/**
 输入框文本判断处理
 @param textField 输入框
 @param range 区间
 @param string 新增文本
 @param placeholder 提示
 */
+(BOOL)judgeTextF:(UITextField *)textField range:(NSRange)range replaceString:(NSString *)string placeholder:(NSString *)placeholder{
    
    if ([placeholder containsString:@"联系方式"]&&[placeholder containsString:@"请输入"]) {
        //包括座机等
        if (string.length == 0){
            return YES;
        }
        if (textField.text.length >= 24) {
            return NO;
        }
        return [ZMSpeedy validateStr:string belongToStr:@"1234567890-+"];
    }
    
    if ([placeholder containsString:@"手机号"]&&[placeholder containsString:@"请输入"]) {
        if (string.length == 0){
            return YES;
        }
        if (textField.text.length >= 11) {
            return NO;
        }
        return [ZMSpeedy validateNumber:string];
    }
    if ([placeholder containsString:@"请输入"]&&([placeholder containsString:@"验证码"]||[placeholder containsString:@"支付密码"])) {
        if (string.length == 0)
        {
            return YES;
        }
        if (textField.text.length > 5) {
            return NO;
        }
        return [ZMSpeedy validateNumber:string];
    }
    
    if ([placeholder containsString:@"请输入"]&&[placeholder containsString:@"密码"]) {
        if (string.length == 0){
            return YES;
        }
        if (textField.text.length >= 18) {
            return NO;
        }
        return [ZMSpeedy validateArabNumOrAddSymbol:string];
    }
    
    
    if (([placeholder containsString:@"请输入"]&&[placeholder containsString:@"金额"]) || ([placeholder containsString:@"请填写"]&&[placeholder containsString:@"售价"])) {
        if (string.length == 0){
            return YES;
        }
        NSString *firStr = [textField.text componentsSeparatedByString:@"."].firstObject;
        if (firStr.length >= 7) {
            return NO;
        }
        
        if ([textField.text containsString:@"."]) {
            if ([string isEqualToString:@"."]) {
                return NO;//限制只有一个小数点
            }
            NSString *lastStr = [textField.text componentsSeparatedByString:@"."].lastObject;
            if (lastStr.length > 1) {
                return NO;//限制两位小数
            }
        }
        return [ZMSpeedy validateArabNumOrAddSymbol:string];
    }
    
    if ([placeholder isEqualToString:@"银行卡号"]) {
        return [ZMSpeedy bankCardNumLayoutAndLengthLimit:textField inRange:range replacementString:string endBlock:nil];
    }
    
    
    if ([placeholder containsString:@"请输入"]&&[placeholder containsString:@"身份证"]) {
        return [ZMSpeedy idCardLayoutAndLengthLimit:textField replacementString:string];
    }
    
    if ([placeholder containsString:@"比例"]) {
        if (string.length == 0) {
            return YES;
        }
        if ([ZMSpeedy validateNumber:string]&&textField.text.length < 3) {
            if (textField.text.length == 3&&![textField.text isEqualToString:@"100"]) {
                return NO;
            }else{
                return YES;
            }
        }else{
            return NO;
        }
    }
    
    if ([placeholder containsString:@"请输入"]&&[placeholder containsString:@"数量"]) {
        if (string.length == 0) {
            return YES;
        }
        if ([ZMSpeedy validateNumber:string]) {
            return YES;
        }else{
            return NO;
        }
    }
    
    return YES;
    
}

/// 距离格式化处理（distance：km）
+(NSString *)zm_getCalDistance:(NSString *)distance{
    NSString *str = @"";
    if ([distance integerValue] > 0) {
        str = kQuickNSString(@"%.2lfkm", [distance floatValue]);
    }else{
        str = kQuickNSString(@"%.0lfm", [distance floatValue]*1000);
    }
    return str;
}


/**
设置富文本填充｜边框
@param type 类型 ：0、填充 1、边框
@param color 圆角度
@param radius 边宽度
*/
+(YYTextBorder *)zm_configRichTextBoder_type:(NSInteger)type color:(UIColor *)color radius:(CGFloat)radius{
    //字体边框
    YYTextBorder *border = [YYTextBorder new];
    //边框圆角
    border.cornerRadius = radius;
    // 填充
    if (type == 0) {
        //填充色
        border.fillColor = color;
    }else if (type == 1) {// 边框
        //边框线宽
        border.strokeWidth = 1;
        //边框颜色等于字体颜色
        border.strokeColor = kMainColor;
    }
    //边框边距
    border.insets = UIEdgeInsetsMake(0,-2, 0,-2);
    border.lineStyle = YYTextLineStyleSingle;
    
    return border;
}


@end
